package com.wltt.product.ahuthr.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

public abstract class Base64Util {

    final static Base64.Encoder encoder = Base64.getEncoder();
    final static Base64.Decoder decoder = Base64.getDecoder();

    /**
     * Base64加密
     *
     * @param text
     * @return
     */
    public static String encode(String text) throws UnsupportedEncodingException {
        byte[] textByte = text.getBytes("UTF-8");
        return encoder.encodeToString(textByte);
    }

    /**
     * Base64解密
     *
     * @param baseStr
     * @return
     */
    public static String decode(String baseStr) throws UnsupportedEncodingException {
        return new String(decoder.decode(baseStr), "UTF-8");
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String text = "你好！";
        String baseStr = encode(text);
        System.out.println(baseStr);
        System.out.println(decode(baseStr));
        System.out.println(decode("ZHNhZGE="));
    }
}
